﻿@using Microsoft.AspNetCore.SignalR.Client
@using TrackSystem.IO.Equip
@using TrackSystem.IO.Formula
@using BootstrapBlazor.Components
@using TrackSystem.UI.Server.Features
@using TrackSystem.UI.Server.Units.UserButton

<Card>
    <HeaderTemplate>
        <p class="card-text">设备名称: @equip.Name</p>
    </HeaderTemplate>

    <BodyTemplate>
        <p class="card-text">设备描述: @equip.Description</p>
        @if (equip.Parts is not null)
        {
            <Divider>传感器</Divider>
            <div class="row g-2">
                @foreach (var item in equip.Parts.Values)
                {
                     if (item.Catogery == EquipPartCatogery.SENSOR)
                    {
                         <div class="col-4">
                            <StatusButton PartAttr="item" CurrentName="@partSelect.CurrentName" OnPartChanged="@(() => OnBtnSelected(item.Name))"></StatusButton>
                        </div>
                    }
                }
            </div>
        }

        @if (equip.Parts is not null)
        {
            <Divider>逻辑程序</Divider>
            <div class="row g-2">
                @foreach (var item in equip.Parts.Values)
                {
                    if (item.Catogery == EquipPartCatogery.LOGIC)
                    {
                        <div class="col-4">
                            <StatusButton PartAttr = "item" CurrentName="@partSelect.CurrentName" OnPartChanged="@(() => OnBtnSelected(item.Name))"></StatusButton>
                        </div>
                    }
                }
            </div>
        }

        @if (equip.Parts is not null)
        {
            <Divider>调节回路</Divider>
            <div class="row g-2">
                @foreach (var item in equip.Parts.Values)
                {
                    if (item.Catogery == EquipPartCatogery.LOOP)
                    {
                        <div class="col-4">
                            <StatusButton PartAttr="item" CurrentName="@partSelect.CurrentName" OnPartChanged="@(() => OnBtnSelected(item.Name))"></StatusButton>
                        </div>
                    }
                }
            </div>

        }
    </BodyTemplate>

</Card>


@code {
    [Parameter]
    public EquipItem equip { get; set; }

    [Parameter]
    public HubConnection hub { get; set; }

    [Parameter]
    public PartSelect  partSelect { get; set; }

    /// <summary>
    /// 修改值
    /// </summary>
    /// <param name="name"></param>
    /// <param name="flag"></param>
    /// <returns></returns>
    private async Task SetValue(string name, bool flag)
    {
        try
        {
            if (hub.State == HubConnectionState.Connected)
                await hub.SendAsync("Assign", $"{name}_Enable", new OperandDef(flag));
        }
        catch (Exception _)
        {
            // ignored
        }
    }

    private Task OnBtnSelected(string name)
    {
        if (name == partSelect.CurrentName)
            partSelect.CurrentName = "";
        else
            partSelect.CurrentName = name;
        return Task.CompletedTask;
    }
}
